package com.huawei.rbac.test;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author 罗华
 * @date 2022/4/15
 */
public class MyPanel extends JPanel implements ActionListener {


    JTextField text;

    int width, height;

    int posX, posY, radius;


    public MyPanel() {

        setLayout(new BorderLayout());
        text = new JTextField(10);
        add(text, BorderLayout.NORTH);
        text.addActionListener(this);

    }

    protected void paintComponent(Graphics g) {
        g.setColor(Color.red);
        g.drawOval(posX, posY, radius * 2, radius * 2);
    }

    /**
     * 发生动作时调用
     *
     * @param e
     */
    public void actionPerformed(ActionEvent e) {

        if (e.getSource() == text) {
            int r = Integer.parseInt(text.getText());
            width = this.getWidth();
            height = this.getHeight();
            posX = width / 2 - r;
            posY = height / 2 - r;
            radius = r;
            repaint();
        }
    }

    public static void main(String[] args) {

        JFrame jFrame = new JFrame();
        jFrame.getContentPane().add(new MyPanel());
        jFrame.setSize(200, 250);
        jFrame.setLocation(10, 100);
        jFrame.setVisible(true);
    }
}
